package top.zhongyy.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import top.zhongyy.mapper.TypeMapper;
import top.zhongyy.pojo.Blog;
import top.zhongyy.pojo.Type;
import top.zhongyy.service.BlogService;
import top.zhongyy.service.TypeService;
import top.zhongyy.vo.BlogQuery;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Andy
 * @since 2021-03-25
 */
@Controller
public class TypeShowController {
    
    @Autowired
    private TypeService typeService;
    
    @Autowired
    private BlogService blogService;
    
    @ApiOperation(value = "跳转到博客分类展示页面")
    @GetMapping("/types/{id}")
    public String types(@RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum,
                        Model model, @PathVariable("id") Long id) {
        List<Type> types = typeService.getAllType();
        if (id == -1) {
            id = types.get(0).getId();
        }
        PageHelper.startPage(pageNum, 5, "update_time DESC");
        List<Blog> blogs = blogService.getBlogByType(id);
        PageInfo<Blog> pageInfo = new PageInfo<>(blogs);
        model.addAttribute("types", types);
        model.addAttribute("activeTypeId", id);
        model.addAttribute("page", pageInfo);
        return "types";
    }
    
    
}

